//
//  MyFile.m
//  Exam11-1
//
//  Created by 孙晓晔 on 2017/4/14.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "MyFile.h"

@implementation MyFile

// 初始化
-(instancetype)initWithDirectory:(NSString*)dir {
    self = [super init];
    if (self) {
        self.file = [NSFileManager defaultManager];
        self.dir = dir;
    }
    
    return self;
}

// 创建目录
-(BOOL)createDir:(NSString *)dir {
    
    NSString *strTmp = [self.dir stringByAppendingString:dir];
    
    NSError *err = nil;
    
    BOOL ret = [self.file createDirectoryAtPath:strTmp withIntermediateDirectories:YES attributes:nil error:&err];
    
    if (err) {
        NSLog(@"%@", err);
    }
    
    return ret;
}

// 创建文件
-(BOOL)createFile:(NSString*)path {
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    BOOL bExist = [self.file fileExistsAtPath:strFile];
    
    if (!bExist) {
        
        return [self.file createFileAtPath:strFile contents:nil attributes:nil];
    }
    
    return FALSE;
}

// 字符串写文件
-(BOOL)writeFile:(NSString*)path str:(NSString*)str {
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    NSError *err = nil;
    
    BOOL ret = [str writeToFile:strFile atomically:NO encoding:(NSUTF8StringEncoding) error:&err];
    
    if (err) {
        NSLog(@"%@", err);
    }
    
    return ret;
}


-(BOOL)writeFile:(NSString*)path data:(NSData*)data {
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    BOOL bExist = [self.file fileExistsAtPath:strFile];
    
    if (!bExist) {
        
        return [self.file createFileAtPath:strFile contents:data attributes:nil];
    } else {
        
        return [data writeToFile:strFile atomically:YES];
    }
}

// 字典写文件
-(BOOL)writeFile:(NSString*)path dict:(NSDictionary*)dict {
    
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    return [dict writeToFile:strFile atomically:YES];
}

// 数据写文件
-(BOOL)writeFile:(NSString*)path arr:(NSArray*)arr {
    
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    return [arr writeToFile:strFile atomically:YES];
}

// 读文件
-(NSString*)readFile:(NSString*)path {
    
    NSString *strFile = [self.dir stringByAppendingString:path];

    NSError *err = nil;
    NSString *content = [NSString stringWithContentsOfFile:strFile encoding:NSUTF8StringEncoding error:&err];
    
    if (err) {
        NSLog(@"%@", err);
    }
    
    NSLog(@"read success: %@",content);
    
    return content;
}

// 移动文件
-(BOOL)moveFile:(NSString*)path dstPath:(NSString*)dstPath {
    
    NSError *err;
    
    BOOL ret = [self.file moveItemAtPath:path toPath:dstPath error:&err];
    
    if (err) {
        NSLog(@"%@", err);
    }
    
    return ret;
    
}

// 删除文件
-(BOOL)deleteFile:(NSString*)path {
    
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    NSError *err = nil;
    
    BOOL ret = [self.file removeItemAtPath:strFile error:&err];
    
    if (err) {
        NSLog(@"%@", err);
    }
    
    return ret;
}

// 判断文件是否存在
-(BOOL)isExistAtPath:(NSString *)path {
    
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    return [self.file fileExistsAtPath:strFile];
}

// 获取文件大小
-(unsigned long long)fileSizeAtPath:(NSString *)path {
    NSString *strFile = [self.dir stringByAppendingString:path];
    
    BOOL isExist = [self.file fileExistsAtPath:strFile];
    if (isExist){
        
        NSError *err = nil;
        
        unsigned long long ret = [[self.file attributesOfItemAtPath:strFile error:&err] fileSize];
        
        if (err) {
            NSLog(@"%@", err);
        }
        
        return ret;
    } else {
        NSLog(@"file is not exist");
        return 0;
    }
}

// 获取指定目录下文件大小
-(unsigned long long)folderSizeAtPath:(NSString*)folderPath {
    BOOL isExist = [self.file fileExistsAtPath:folderPath];
    
    if (isExist){
        NSEnumerator *childFileEnumerator = [[self.file subpathsAtPath:folderPath] objectEnumerator];
        unsigned long long ret = 0;
        NSString *fileName = @"";
        
        while ((fileName = [childFileEnumerator nextObject]) != nil){
            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            NSError *err = nil;
            
            ret += [[self.file attributesOfItemAtPath:fileAbsolutePath error:&err] fileSize];
        }
        
        return ret;
    } else {
        NSLog(@"file is not exist");
        return 0;
    }
}

// 列出目录下文件
-(NSArray *)listAtPath:(NSString*)folderPath {
    BOOL isExist = [self.file fileExistsAtPath:folderPath];
    
    if (isExist){
        NSEnumerator *childFileEnumerator = [[self.file subpathsAtPath:folderPath] objectEnumerator];
        NSString *fileName = @"";
        
        NSMutableArray *arr = [NSMutableArray array];
        
        while ((fileName = [childFileEnumerator nextObject]) != nil){
            NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
            
            [arr addObject:fileAbsolutePath];
            // folderSize += [self fileSizeAtPath:fileAbsolutePath];
        }
        
        return arr;
    } else {
        NSLog(@"file is not exist");
        return nil;
    }
}

@end
